package com.lp.biz.train.controller;

import com.lp.biz.train.model.query.TrainChapterQuery;
import com.lp.biz.train.model.query.TrainClassQuery;
import com.lp.biz.train.model.query.TrainHomeworkQuery;
import com.lp.biz.train.model.query.TrainMembersQuery;
import com.lp.biz.train.model.vo.*;
import com.lp.biz.train.service.BizTrainService;
import com.lp.common.annotation.PermissionAnnotation;
import com.lp.common.entity.PageParams;
import com.lp.common.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.lp.common.constant.IdentityConstant.*;
import static com.lp.common.constant.MenuConstant.CONTENT_TRAINING;
import static com.lp.common.constant.MenuConstant.USER_TRAINING_STUDENT;

/**
 * BizCoreContentController
 * @program: backend
 * @author Ke.Song
 * @since 2024-05-02 10:45:20
 */
@RestController
@RequestMapping("/train")
public class BizTrainController {

    @Autowired
    private BizTrainService trainService;

    /**
     * 获取训练营列表
     * @param params 分页参数
     * @return 查询结果
     */
    @GetMapping
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getTrainList(PageParams params) {
        return trainService.getTrainList(params);
    }

    /**
     * 创建训练营
     * @param createTrain 创建参数
     * @return 处理结果
     */
    @PostMapping()
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult createTrain(@RequestBody@Validated CreateTrain createTrain) {
        trainService.createTrain(createTrain);
        return CommonResult.success();
    }

    /**
     * 删除训练营
     * @param trainId 训练营id
     * @return 处理结果
     */
    @DeleteMapping("/{trainId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult deleteTrain(@PathVariable Integer trainId) {
        trainService.deleteTrain(trainId);
        return CommonResult.success();
    }

    /**
     * 训练营上架/下架
     * @param trainId 训练营ID
     * @return 处理结果
     */
    @PutMapping("/{trainId}/display")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult trainDisplay(@PathVariable Integer trainId) {
        trainService.trainDisplay(trainId);
        return CommonResult.success();
    }

    /**
     * 获取训练营详情
     * @param trainId 训练营id
     * @return 查询结果
     */
    @GetMapping("/{trainId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getTrainDetail(@PathVariable Integer trainId) {
        return trainService.getTrainDetail(trainId);
    }

    /**
     * 获取训练营班级
     *
     * @param trainId 训练营id
     * @param query 分页参数
     * @return 查询结果
     */
    @GetMapping("/{trainId}/class")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getTrainClasses(@PathVariable Integer trainId, TrainClassQuery query) {
        query.setTrainId(trainId);
        return trainService.getTrainClasses(query);
    }

    /**
     * 创建/更新班级
     * @param trainId 训练营ID
     * @param createClass 班级参数
     * @return 处理结果
     */
    @PostMapping("/{trainId}/class")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult addOrUpdateClass(@PathVariable Integer trainId, @RequestBody@Validated CreateClass createClass) {
        trainService.addOrUpdateClass(trainId, createClass);
        return CommonResult.success();
    }

    /**
     * 上架/下架班级
     * @param trainId 训练营ID
     * @param classId 班级id
     * @return 处理结果
     */
    @PutMapping("/{trainId}/class/{classId}/display")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult displayClass(@PathVariable Integer trainId, @PathVariable Integer classId) {
        trainService.displayClass(trainId, classId);
        return CommonResult.success();
    }

    /**
     * 获取训练营下班级信息
     * @param classId 班级id
     * @param trainId 训练营id
     * @return 查询结果
     */
    @GetMapping("/{trainId}/class/{classId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getTrainClass(@PathVariable Integer classId, @PathVariable Integer trainId) {
        return trainService.getTrainClass(classId, trainId);
    }

    /**
     * 获取训练营的章节
     * @param trainId 训练营id
     * @param query 查询参数
     * @return 查询结果
     */
    @GetMapping("/{trainId}/chapter")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getTrainChapterList(@PathVariable Integer trainId, TrainChapterQuery query) {
        return trainService.getTrainChapterList(trainId, query);
    }

    /**
     * 创建章节
     * @param trainId 训练营id
     * @param params 章节参数
     * @return 处理结果
     */
    @PostMapping("/{trainId}/chapter")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult addChapter(@PathVariable Integer trainId, @RequestBody@Validated TrainChapterCreate params) {
        trainService.addOrUpdateChapter(trainId, params);
        return CommonResult.success();
    }

    /**
     * 更新章节
     * @param trainId 训练营id
     * @param params 章节参数
     * @return 处理结果
     */
    @PutMapping("/{trainId}/chapter/{chapterId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult updateChapter(@PathVariable Integer trainId, @PathVariable Integer chapterId,
                                      @RequestBody@Validated TrainChapterCreate params) {
        params.setId(chapterId);
        trainService.addOrUpdateChapter(trainId, params);
        return CommonResult.success();
    }

    /**
     * 删除章节
     * @param trainId 训练营ID
     * @param chapterId 章节ID
     * @return 处理结果
     */
    @DeleteMapping("/{trainId}/chapter/{chapterId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult deleteChapter(@PathVariable Integer trainId, @PathVariable Integer chapterId) {
        trainService.deleteChapter(trainId, chapterId);
        return CommonResult.success();
    }

    /**
     * 获取章节详情
     * @param chapterId 章节ID
     * @param trainId 训练营ID
     * @return 查询结果
     */
    @GetMapping("/{trainId}/chapter/{chapterId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getChapter(@PathVariable Integer chapterId, @PathVariable Integer trainId) {
        return trainService.getChapter(chapterId, trainId);
    }


    /**
     * 训练营章节上架/下架
     * @param chapterId 章节id
     * @return 处理结果
     */
    @PutMapping("/chapter/{chapterId}/display")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult displayChapter(@PathVariable Integer chapterId) {
        trainService.displayChapter(chapterId);
        return CommonResult.success();
    }

    /**
     * 获取训练营下的用户
     * @param trainId 训练营Id
     * @param query 查询参数
     * @return 查询结果
     */
    @GetMapping("/{trainId}/members")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getAllMembers(@PathVariable Integer trainId, TrainMembersQuery query) {
        return trainService.getAllMembers(trainId, query);
    }

    /**
     * 删除训练营下的用户班级
     *
     * @param trainId       训练营Id
     * @param membersDelete 删除参数
     * @return 查询结果
     */
    @DeleteMapping("/{trainId}/members")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult deleteTrainMembers(@PathVariable Integer trainId, @RequestBody @Validated TrainMembersDelete membersDelete) {
        trainService.deleteTrainMembers(trainId, membersDelete);
        return CommonResult.success();
    }

    /**
     * 获取可选班主任
     * @param name 班主任
     * @return 查询结果
     */
    @GetMapping("/class-teacher")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getOptionalClassTeacher(String name) {
        return trainService.getOptionalManage(null, name, CLASS_TEACHER);
    }

    /**
     * 获取训练营下可选助教
     * @param trainId 训练营Id
     * @param name 助教名
     * @return 查询结果
     */
    @GetMapping("/{trainId}/assistant")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getOptionalAssistant(@PathVariable Integer trainId, String name) {
        return trainService.getOptionalManage(trainId, name, CATEGORY_ASSISTANT);
    }

    /**
     * 获取训练营下可选导师
     * @param trainId 训练营Id
     * @param name 导师
     * @return 查询结果
     */
    @GetMapping("/{trainId}/mentor")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getOptionalMentor(@PathVariable Integer trainId, String name) {
        return trainService.getOptionalManage(trainId, name, CATEGORY_MENTOR);
    }

    /**
     * 获取训练营下可选教练
     * @param trainId 训练营Id
     * @param name 教练
     * @return 查询结果
     */
    @GetMapping("/{trainId}/trainer")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getOptionalTrainer(@PathVariable Integer trainId, String name) {
        return trainService.getOptionalManage(trainId, name, CATEGORY_TRAINER);
    }

    /**
     * 获取当前训练营的可选班级
     * @param trainId 训练营Id
     * @param name 班主任
     * @return 查询结果
     */
    @GetMapping("/{trainId}/classes")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getOptionalTrainClasses(String name, @PathVariable Integer trainId) {
        return trainService.getOptionalTrainClasses(trainId, name);
    }

    /**
     * 分配班级
     * @param assignMember 学员列表
     * @param trainId 训练营ID
     * @return 处理结果
     */
    @PostMapping("/{trainId}/assign")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult assignToClass(@RequestBody@Validated TrainAssignMember assignMember,
                                      @PathVariable Integer trainId) {
        trainService.assignToClass(assignMember, trainId);
        return CommonResult.success();
    }

    /**
     * 创建作业
     * @param trainId 训练营id
     * @param params 作业参数
     * @return 处理结果
     */
    @PostMapping("/{trainId}/homework")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult addHomework(@PathVariable Integer trainId,
                                            @RequestBody@Validated TrainHomeworkCreateOrUpdate params) {
        trainService.addOrUpdateHomework(trainId, params);
        return CommonResult.success();
    }

    /**
     * 更新作业
     * @param trainId 训练营id
     * @param homeworkId 作业id
     * @param params 作业参数
     * @return 处理结果
     */
    @PutMapping("/{trainId}/homework/{homeworkId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult updateHomework(@PathVariable Integer trainId, @PathVariable Integer homeworkId,
                                       @RequestBody@Validated TrainHomeworkCreateOrUpdate params) {
        params.setId(homeworkId);
        trainService.addOrUpdateHomework(trainId, params);
        return CommonResult.success();
    }

    /**
     * 获取训练营作业列表
     * @param trainId 训练营Id
     * @param query 分页参数
     * @return 查询结果
     */
    @GetMapping("/{trainId}/homework")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult getTrainHomework(@PathVariable Integer trainId, TrainHomeworkQuery query) {
        return trainService.getTrainHomework(trainId, query);
    }

    /**
     * 删除训练营作业
     * @param homeworkId 作业id
     * @param trainId 训练营id
     * @return 处理结果
     */
    @DeleteMapping("/{trainId}/homework/{homeworkId}")
    @PermissionAnnotation(menus = {CONTENT_TRAINING})
    public CommonResult deleteHomework(@PathVariable Integer homeworkId, @PathVariable Integer trainId) {
        trainService.deleteHomework(homeworkId, trainId);
        return CommonResult.success();
    }

    /**
     * 获取训练营下拉
     * @param search 训练营名字
     * @return 查询结果
     */
    @GetMapping("/down")
    @PermissionAnnotation(menus = {CONTENT_TRAINING, USER_TRAINING_STUDENT})
    public CommonResult getTrainDown(String search) {
        return trainService.getTrainDown(search);
    }

    /**
     * 获取训练营类型
     * @return 查询结果
     */
    @GetMapping("/type")
    @PermissionAnnotation
    public CommonResult getTrainType() {
        return trainService.getTrainType();
    }
}
